﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imagine;
using Box2D.XNA;
using Microsoft.Xna.Framework;

namespace Carmack
{
    public class CActor: GameObject,IContactListener
    {
        protected uint m_health;
        protected bool m_dead;
        protected bool m_active;
        protected float m_rotationAngle;
        protected Vector2 m_velocity;
        protected Vector2 m_faceDirection;
        public PhysicsSprite ActorPhysics;
        
        public CActor(string texture)
        {
            ActorPhysics = new PhysicsSprite(texture);
            m_health = 100;
            m_dead = false;
            m_active = true;
            m_rotationAngle = 0.0f;
            //m_faceDirection = new Vector2( 0, 1 );
            m_faceDirection.X=0;
            m_faceDirection.Y=1;
        }

        #region Properties

        public Vector2 Velocity
        {
            get { return m_velocity;  }
            set { m_velocity = value; }
        }

        public Vector2 Direction
        {
            get { return m_faceDirection;  }
            set { m_faceDirection = value; }
        }

        public float RotationAngle
        {
            get { return m_rotationAngle;  }
            set { m_rotationAngle = value; }
        }

        public uint Health
        {
           get { return m_health; }
           set 
           { 
               m_health = value;
               if( m_health >= 100 )      m_health = 100;
               else if ( m_health <= 0 )  m_health = 0;
           }
        }

        public bool IsActive
        {
            get { return m_active;  }
            set { m_active = value; }
        }

        public bool IsDead
        {
            get { return (m_health == 0);  }
            set { m_dead = value; }
        }
        #endregion

        public override bool Update(GameTime time)
        {
            ActorPhysics.Update(time);
            return true;
        }

        public override bool Draw(GameTime time) 
        {
            ActorPhysics.Draw(time);
            return true; 
        }

        public void BeginContact(Contact contact) { }
        public void EndContact(Contact contact) { }
        public void PreSolve(Contact contact, ref Manifold oldManifold) { }
        public void PostSolve(Contact contact, ref ContactImpulse impulse) { }

        #region Protected Members
        protected float VectorToAngle( Vector2 target )
        {
            m_faceDirection = target - ActorPhysics.Position;
            m_faceDirection.Normalize();
            return (float)Math.Atan2(m_faceDirection.X, -m_faceDirection.Y);
        }
        #endregion
    }
    
}
